﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeNotes.Common
{
    public class Log
    {
        /// <summary>
        /// 错误信息记录
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public static void LogNote(string title, string content, string appName)
        {
            try
            {
                string filepath = System.Web.HttpContext.Current.Server.MapPath("~");
                if (!System.IO.Directory.Exists(filepath + @"\log"))
                    System.IO.Directory.CreateDirectory(filepath + @"\log");
                if (appName != string.Empty)
                {
                    if (!System.IO.Directory.Exists(filepath + @"\log\" + appName))
                        System.IO.Directory.CreateDirectory(filepath + @"\log\" + appName);
                    FileAdd(filepath + @"\log\" + appName + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString() + " " + "[" + title + "]" + content + "\r\n");
                }
                else
                {
                    FileAdd(filepath + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString() + " " + "[" + title + "]" + content + "\r\n");
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 追加文件
        /// </summary>
        /// <param name="Path"> 文件路径</param>
        /// <param name="strings"> 内容</param>
        private static void FileAdd(string Path, string strings)
        {
            StreamWriter sw = File.AppendText(Path);
            sw.Write(strings);
            sw.Flush();
            sw.Close();
        }

    }
}
